package middle;

public class Solution_129 {

    private int sum = 0;
    public int sumNumbers(TreeNode root) {
        dps(root, 0);
        return sum;
    }

    public void dps(TreeNode root, int base) {
        int num = base * 10 + root.val;
        if (root.left == null && root.right == null) {
            sum += num;
            System.out.println(num);
            return;
        }
        if (root.left != null) {
            dps(root.left, num);
        }
        if (root.right != null) {
            dps(root.right, num);
        }
    }

    static class TreeNode {

        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
